package com.braveman.im.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.DataSearchBean;
import com.braveman.im.bean.HomeSearchBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.message.ChatAllActivity;
import com.braveman.im.ui.adapter.HomeSearchAdapter;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class HomeSearchActivity extends BaseActivity {
    private RecyclerView mLvActivityListMember;
    private LoadingLayout loadingLayout;
    private EditText mCetActivityAddFriendsSearch;
    private HomeSearchAdapter homeSearchAdapter;
    private List<DataSearchBean> allDates = new ArrayList<>();
    private List<DataSearchBean> companiesList = new ArrayList<>(); //所有平台
    private List<DataSearchBean> friendsBeanList = new ArrayList<>();//所有好友
    private List<DataSearchBean> groupsBeanList = new ArrayList<>();//所有群组

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected boolean isFulleScreenButAnv() {
        return true;
    }

    @Override
    protected void initTitleBar() {

    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_home_search;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
    }

    @Override
    protected void initChildView() {
        loadingLayout = findViewById(R.id.loading_layout);
        homeSearchAdapter = new HomeSearchAdapter(R.layout.item_home_search, R.layout.item_home_search_head, allDates);
        mLvActivityListMember = findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(this));
        mCetActivityAddFriendsSearch = findViewById(R.id.cet_activity_add_friends_search);
        mLvActivityListMember.setAdapter(homeSearchAdapter);
        loadingLayout.showEmpty();
    }

    @Override
    protected void initChildData() {

    }

    @Override
    protected void setChildViewListener() {

        mCetActivityAddFriendsSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == 3) {
                /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(textView.getApplicationWindowToken(), 0);
                    }

                    doNetwork();
                    return true;
                }
                return false;
            }
        });
        findViewById(R.id.tv_activity_search_back).setOnClickListener(this);
        homeSearchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                if (allDates.get(position).isHeader) {
                    return;
                }
                if (position >= 0 && position <= groupsBeanList.size()) {
                    bundle.putInt("states", 1);
                    bundle.putString("groupId", allDates.get(position).groupId);
                    bundle.putString("user_id", allDates.get(position).imGroupId);
                } else if (position > groupsBeanList.size() && position < groupsBeanList.size() + companiesList.size()) {
                    bundle.putInt("states", 2);
                    bundle.putString("groupId", allDates.get(position).groupId);
                    bundle.putString("user_id", allDates.get(position).imGroupId);
                } else if (position > groupsBeanList.size() + companiesList.size()) {
                    bundle.putInt("states", 3);
                    bundle.putString("user_id", allDates.get(position).memberImUsername);
                }
                ActivityUtils.startActivityAndFinish(HomeSearchActivity.this, ChatAllActivity.class, bundle);
            }
        });

        homeSearchAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.rl_see_more:
                        if ("group".equals(allDates.get(position).dataType)) {
                            allDates.clear();
                            allDates.add(new DataSearchBean(true, "群组", false));
                            allDates.addAll(groupsBeanList);
                            for (DataSearchBean da : allDates) {
                                da.isFoot = false;
                            }
                            getCompanList();
                            getFriendsList();
                            if (homeSearchAdapter != null) {
                                homeSearchAdapter.notifyDataSetChanged();
                            }
                            if (allDates.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                        } else if ("company".equals(allDates.get(position).dataType)) {
                            allDates.clear();
                            getGroupList();
                            allDates.add(new DataSearchBean(true, "单位", true));
                            allDates.addAll(companiesList);
                            for (DataSearchBean da : allDates) {
                                if ("company".equals(da.dataType))
                                    da.isFoot = false;
                            }
                            getFriendsList();
                            if (homeSearchAdapter != null) {
                                homeSearchAdapter.notifyDataSetChanged();
                            }
                            if (allDates.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                        } else if ("friend".equals(allDates.get(position).dataType)) {
                            allDates.clear();
                            getGroupList();
                            getCompanList();
                            allDates.add(new DataSearchBean(true, "好友", true));
                            allDates.addAll(friendsBeanList);
                            for (DataSearchBean da : allDates) {
                                if ("friend".equals(da.dataType))
                                    da.isFoot = false;
                            }
                            if (homeSearchAdapter != null) {
                                homeSearchAdapter.notifyDataSetChanged();
                            }
                            if (allDates.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                        }
                        break;
                }
            }
        });
    }

    private void doNetwork() {
        String search = mCetActivityAddFriendsSearch.getText().toString().trim();
        loadingLayout.showLoading();
        groupsBeanList.clear();
        companiesList.clear();
        friendsBeanList.clear();
        ApiManager.getApi().homeSearch(search, 1, 50).enqueue(new NetCallBack<HomeSearchBean>() {
            @Override
            protected void onSuccess(Call<HomeSearchBean> call, Response<HomeSearchBean> response) {
                if (response != null) {
                    HomeSearchBean homeSearchBean = response.body();
                    groupsBeanList.addAll(homeSearchBean.groups.dataList);
                    companiesList.addAll(homeSearchBean.companies.dataList);
                    friendsBeanList.addAll(homeSearchBean.friends.dataList);
                    if (allDates != null) {
                        allDates.clear();
                    }

                    getGroupList();
                    getCompanList();
                    getFriendsList();

                    if (homeSearchAdapter != null) {
                        homeSearchAdapter.notifyDataSetChanged();
                    }
                    if (allDates.size() == 0) {
                        loadingLayout.showEmpty();
                    } else {
                        loadingLayout.showContent();
                    }
                } else {

                    loadingLayout.showError();
                }
            }

            @Override
            protected void hiddenProgress() {
                hideLodingDialog();
            }

            @Override
            public void onFailure(Call<HomeSearchBean> call, Throwable t) {
                super.onFailure(call, t);
                loadingLayout.showError();
            }
        });
    }

    private void getFriendsList() {
        if (friendsBeanList != null && friendsBeanList.size() > 0) {
            allDates.add(new DataSearchBean(true, "好友", true));
            if (friendsBeanList.size() > 3) {
                allDates.add(friendsBeanList.get(0));
                allDates.add(friendsBeanList.get(1));
                allDates.add(friendsBeanList.get(2));
                allDates.get(allDates.size() - 1).isFoot = true;
            } else {
                allDates.addAll(friendsBeanList);
            }
        }
    }

    private void getCompanList() {
        if (companiesList != null && companiesList.size() > 0) {
            allDates.add(new DataSearchBean(true, "单位", true));
            if (companiesList.size() > 3) {
                allDates.add(companiesList.get(0));
                allDates.add(companiesList.get(1));
                allDates.add(companiesList.get(2));
                allDates.get(allDates.size() - 1).isFoot = true;
            } else {
                allDates.addAll(companiesList);
            }
        }
    }

    private void doNetwork(final int type) {
        String search = mCetActivityAddFriendsSearch.getText().toString().trim();
        loadingLayout.showLoading();
        groupsBeanList.clear();
        companiesList.clear();
        friendsBeanList.clear();
        ApiManager.getApi().homeSearch(search, 1, 50).enqueue(new NetCallBack<HomeSearchBean>() {
            @Override
            protected void onSuccess(Call<HomeSearchBean> call, Response<HomeSearchBean> response) {
                if (response != null) {
                    HomeSearchBean homeSearchBean = response.body();
                    groupsBeanList.addAll(homeSearchBean.groups.dataList);
                    companiesList.addAll(homeSearchBean.companies.dataList);
                    friendsBeanList.addAll(homeSearchBean.friends.dataList);
                    if (allDates != null) {
                        allDates.clear();

                    }

//                    if (groupsBeanList.size() > 0) {

//                    }
//                    if (companiesList.size() > 0) {
//                        allDates.add(new DataSearchBean(true, "单位"));
//                        allDates.addAll(companiesList);
//                    }
//                    if (friendsBeanList.size() > 0) {
//                        allDates.add(new DataSearchBean(true, "好友"));
//                        allDates.addAll(friendsBeanList);
//                    }


                    getGroupList();
                    getCompanList();
                    getFriendsList();

                    if (homeSearchAdapter != null) {
                        homeSearchAdapter.notifyDataSetChanged();
                    }
                    if (allDates.size() == 0) {
                        loadingLayout.showEmpty();
                    } else {
                        loadingLayout.showContent();
                    }
                } else {

                    loadingLayout.showError();
                }
            }

            @Override
            protected void hiddenProgress() {
                hideLodingDialog();
            }

            @Override
            public void onFailure(Call<HomeSearchBean> call, Throwable t) {
                super.onFailure(call, t);
                loadingLayout.showError();
            }
        });
    }

    private void getGroupList() {
        if (groupsBeanList != null && groupsBeanList.size() > 0) {//群
            allDates.add(new DataSearchBean(true, "群组", false));
            if (groupsBeanList.size() > 3) {
                allDates.add(groupsBeanList.get(0));
                allDates.add(groupsBeanList.get(1));
                allDates.add(groupsBeanList.get(2));
                allDates.get(allDates.size() - 1).isFoot = true;
            } else {
                allDates.addAll(groupsBeanList);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_activity_search_back:
                ActivityUtils.finishActivity(HomeSearchActivity.this);
                break;
        }
    }
}
